package com.xinyue.auto.reader.process;

import java.util.List;

import org.dom4j.Element;

import com.xinyue.auto.bean.Query;
import com.xinyue.auto.exception.InterfaceException;
import com.xinyue.auto.reader.Process;
import com.xinyue.auto.reader.major.FindMajor;
import com.xinyue.auto.reader.util.JavaTypeUtil;
import com.xinyue.auto.reader.util.ReturnTypeUtil;
import com.xinyue.auto.type.Type;
import com.xinyue.auto.util.StringUtil;

public class InterfaceProcess extends FindMajor implements Type{
	public InterfaceProcess(Process read) {
		super(read);
	}

	@SuppressWarnings("unchecked")
	@Override
	protected void validate() throws Exception {
		Element find = xml.element(finder_);
		if(find != null) {
			//读取interface标签
			List<Element> itf = find.elements(interface_);
			
			if(itf != null && itf.size() > 0) {
				try {
					for (Element inter: itf) {
						//读取公共属性
						Query q = commonRead(inter);
						q.setType(interType);
						
						//返回值
						interReturn(inter, q);
						//入参
						interParam(inter, q);
						
						querys.add(q);
					}
				} catch (Exception e) {
					throw new InterfaceException(e.getMessage(), holder.getException());
				}
			}
		}
	}
	
	/**
	 * dynamic返回值
	 * @param inter
	 * @param q
	 * @throws Exception
	 */
	private void interReturn(Element inter, Query q) throws Exception {
		String entity = xml.attributeValue(name_);
		
		String type = inter.attributeValue(returnType_);
		String dto = inter.attributeValue(dto_);
		if(StringUtil.isNull(type)) {
			throw new Exception("未定义returnType属性或其值为空");
		}
		
		//如果未定义dto属性，则按一般情况处理，反之其返回值只能是DTO或者LIST
		if(StringUtil.isNull(dto)) {
			if(entity.equals(type)) {
				q.setReturnType(entity);
			}else if(LIST.equals(type)) {
				q.setReturnType("List<" + entity + ">");
			}else if(COUNT.equals(type)) {
				q.setReturnType(LONG_);
			}else {
				String impt = ReturnTypeUtil.validate(type, q);
				if(impt != null) {
					holder.getDaoImpt().add(impt);
				}
			}
		}else {
			if(dto.indexOf(".") < 1) {
				throw new Exception("dto属性值非法，不是标准的Java对象：" + dto);
			}
			
			String bean = StringUtil.getNameByDoc(dto);
			if(DTO.equals(type)) {
				q.setReturnType(bean);
			}else if(LIST.equals(type)) {
				q.setReturnType("List<" + bean + ">");
			}else {
				throw new Exception("定义了dto属性，则返回值只能是DTO或者List");
			}
			
			holder.getDaoImpt().add(dto);
		}
	}
	
	/**
	 * 入参处理
	 * @param inter
	 * @param q
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	private void interParam(Element inter, Query q) throws Exception {
		List<Element> params = inter.elements(query_param_);
		
		//方法入参
		String param = "";
		String paramIn = "";
		
		for (Element pm: params) {
			String name = pm.attributeValue(name_);
			String type = pm.attributeValue(type_);
			
			//name属性验证
			if(StringUtil.isNull(name)) {
				throw new Exception("query-param标签未定义name属性或其值为空");
			}
			//type属性验证
			if(StringUtil.isNull(type)) {
				throw new Exception("query-param标签未定义type属性或其值为空");
			}else if("Map".equals(type)) {
				type = "Map<String, Object>";
				holder.getDaoImpt().add("java.util.Map");
			}else {
				String impt = JavaTypeUtil.validate(type);
				if(impt != null) {
					holder.getDaoImpt().add(impt);
				}
			}
			
			//合并参数
			param += type + " " + name + ", ";
			paramIn += name + ", ";
			
			//参数处理
			param = param.substring(0, param.length() - 2);
			paramIn = paramIn.substring(0, paramIn.length() - 2);
			
			q.setParams(param);
			q.setParamsIn(paramIn);
		}
	}
}